package psd.model.Prolog.Converters;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Name;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologMethodDeclarationConverter extends PrologConverter implements
		IConverter<MethodDeclaration> {

	public PrologMethodDeclarationConverter(FactGenerationToolbox toolbox) {
		super(toolbox);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean convert(MethodDeclaration node) {
		 String id = generationToolBox.getIDResolver().getID(node);
	        String parentId = getParentId(node);
	        String name = PrologCommon.quote(node.isConstructor() ? "<init>" : node.getName().toString());
	        String param = generationToolBox.getIDResolver().getIDs(node.parameters());
	        String type = generationToolBox.getTypeResolver().getTypeTerm(node.resolveBinding().getReturnType());
	        List exs = node.thrownExceptions();
	        List exsBind = new ArrayList();
	        Name ex;
	        for(Iterator iter = exs.iterator(); iter.hasNext(); exsBind.add(ex.resolveBinding()))
	        {
	            ex = (Name)iter.next();
	        }

	        String exceptions = generationToolBox.getIDResolver().getIDs(exsBind);
	        String body = generationToolBox.getIDResolver().getID(node.getBody());
	        String args[] = {
	            id, parentId, name, param, type, exceptions, body, this.getFilePath(node), this.getLineNumber(node)
	        };
	        generationToolBox.getFactWriter().writeFact("methodDefT", args);	        
	        
	        int startAndLength[] = commentHelper.excludeComments(node);
	        writer.writeFact("slT", new String[] {
	            idResolver.getID(node), Integer.toString(startAndLength[0]), Integer.toString(startAndLength[1])
	        });
	        checkAndGenComments(node, id);
	        writeSourceLocationArgumentIdentifier(node, node.getName(), node.modifiers());
	        writeModifiers(node, node.getModifiers());
	        return true;
	}


}
